﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using DAO;
using DevExpress.XtraEditors.Controls;

namespace QLDaiLy.GUI.PhieuThu
{
    public partial class PhieuThuTien : DevExpress.XtraEditors.XtraUserControl
    {
        public PhieuThuTien()
        {
            InitializeComponent();
        }
        private void PhieuThuTien_Load(object sender, EventArgs e)
        {
            LoadDanhSachQuan();
            LoadDanhSachLoaiDaiLy();
            LoadDanhSachDaiLy();
        }
        private void LoadDanhSachDaiLy()
        {
            List<DaiLyDTO> list = new List<DaiLyDTO>();
            list = DaiLyDAO.LoadDanhSachDaiLy();
            foreach (var dly in list)
            {
                cbbTenDaiLy.Properties.Items.Add(dly.TenDaiLy);
            }


        }
        private void LoadDanhSachQuan()
        {
            List<QuanDTO> list = new List<QuanDTO>();
            list = QuanDAO.LoadDanhSachQuan();
            foreach (var quan in list)
            {
                cbbQuan.Properties.Items.Add(quan.TenQuan);
            }

        }
        private void LoadDanhSachLoaiDaiLy()
        {

            List<LoaiDaiLyDTO> list = new List<LoaiDaiLyDTO>();
            list = LoaiDaiLyDAO.LoadDanhSachLoaiDaiLy();
            foreach (var loaidaily in list)
            {
                cbbLoaiDaiLy.Properties.Items.Add(loaidaily.TenLoaiDaiLy);
            }

        }

        private void cbbTenDaiLy_SelectedIndexChanged(object sender, EventArgs e)
        {
           // DaiLyDTO dl = new DaiLyDTO();
            DataTable dt = DaiLyDAO.LoadDanhSachDaiLyTheoTenDaiLy(cbbTenDaiLy.SelectedItem.ToString());
            if (dt.Rows.Count!=0)
            {

              
                    DataRow dr = dt.Rows[0];
                    cbbQuan.Text = dr["TenQuan"].ToString();
                    cbbLoaiDaiLy.Text = dr["TenLoaiDaiLy"].ToString();
                    tbDiaChi.Text = dr["DiaChi"].ToString();
                    tbDienThoai.Text = dr["DienThoai"].ToString();
                    tbNoCuaDaiLy.Text = dr["NoCuaDaiLy"].ToString();

            }
        }

        private void btnThanhToan_Click(object sender, EventArgs e)
        {
          
            DataTable dt = DaiLyDAO.LoadDanhSachDaiLyTheoTenDaiLy(cbbTenDaiLy.SelectedItem.ToString());

            string ng, th, nm;
            ng = dateThuTien.DateTime.Day.ToString();
            th = dateThuTien.DateTime.Month.ToString();
            nm = dateThuTien.DateTime.Year.ToString();

            int madl = dt.Rows[0]["MaDaiLy"].GetHashCode();
            int tienNo = dt.Rows[0]["NoCuaDaiLy"].GetHashCode();
            int thuTien = int.Parse(tbTienThu.Text);

            if (thuTien > tienNo)
            {
                MessageBox.Show("Tiền Thu không vượt quá tiền nợ", "Thông Báo");
                tbTienThu.Focus(); ;
            }
            else
            {
                PhieuThuTienDTO pt = new PhieuThuTienDTO();
                pt.MaDaiLy = madl;
                pt.SoTienThu = thuTien;
                pt.NgayThuTien = ng + "/" + th + "/" + nm;
                
                int NoCuaDaiLy = tienNo - thuTien;
                int MaDaiLy = dt.Rows[0]["MaDaiLy"].GetHashCode();
                bool kq1 = DaiLyDAO.CapNhatNoCuaDaiLy(MaDaiLy, NoCuaDaiLy);
                if (kq1==true)
                {
                    MessageBox.Show("ok");
                    bool kq = PhieuThuTienDAO.ThemPhieuThu(pt);
                    if (kq)
                    {
                        MessageBox.Show("Đã thu tiền", "Thông Báo");
                    }
                    else
                    {
                        MessageBox.Show("Có lỗi trong thanh toán", "Thông Báo");
                    }
                }
                else
                {
                    MessageBox.Show("Có lỗi trong cập nhật tiền nợ", "Thông Báo");
                }

            }
           
        }
    }
}
